package edu.controller;

import edu.service.BaseCourseService;
import edu.vo.BaseCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/base-courses")
public class BaseCourseController {

    @Autowired
    private BaseCourseService baseCourseService;

    /**
     * 保存基础课程信息
     * @param baseCourse 基础课程对象
     * @return 保存后的基础课程对象
     */
    @PostMapping
    public ResponseEntity<BaseCourse> saveBaseCourse(@RequestBody BaseCourse baseCourse) {
        BaseCourse savedBaseCourse = baseCourseService.saveBaseCourse(baseCourse);
        return new ResponseEntity<>(savedBaseCourse, HttpStatus.CREATED);
    }

    /**
     * 根据 ID 获取基础课程信息
     * @param id 基础课程 ID
     * @return 基础课程对象的 Optional 包装
     */
    @GetMapping("/{id}")
    public ResponseEntity<BaseCourse> getBaseCourseById(@PathVariable Integer id) {
        Optional<BaseCourse> baseCourseOptional = baseCourseService.getBaseCourseById(id);
        return baseCourseOptional.map(baseCourse -> new ResponseEntity<>(baseCourse, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 获取所有基础课程信息
     * @return 基础课程列表
     */
    @GetMapping
    public ResponseEntity<List<BaseCourse>> getAllBaseCourses() {
        List<BaseCourse> baseCourses = baseCourseService.getAllBaseCourses();
        return new ResponseEntity<>(baseCourses, HttpStatus.OK);
    }

    /**
     * 根据 ID 删除基础课程信息
     * @param id 基础课程 ID
     * @return 无内容响应
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBaseCourseById(@PathVariable Integer id) {
        baseCourseService.deleteBaseCourseById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 更新基础课程信息
     * @param baseCourse 新的基础课程信息
     * @return 更新后的基础课程对象
     */
    @PutMapping
    public ResponseEntity<BaseCourse> updateBaseCourse(@RequestBody BaseCourse baseCourse) {
        BaseCourse updatedBaseCourse = baseCourseService.updateBaseCourse(baseCourse);
        return updatedBaseCourse != null ? new ResponseEntity<>(updatedBaseCourse, HttpStatus.OK)
                : new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}